<?php

namespace app\models;

use yii\db\ActiveRecord;

/**
 * 卡基本信息
 * Class CardBasic
 * @package app\models
 *
 * @property integer $id PK
 * @property integer $card_id 卡编号
 * @property integer $refresh_time 刷新时间
 * @property string $iccid
 * @property string $terminal_id
 * @property string $modem_id
 * @property string $customer The name of the customer this Terminal belongs to.
 * @property string $end_consumer_id
 * @property string $suspended Suspended: "Y" for Yes, "N" for No, or "F" for Fraud Review
 * @property string $rate_plan The name of the rate plan.
 * @property string $status SIM Status:
 * "TEST_READY_NAME": Test Ready,
 * "INVENTORY_NAME": Inventory,
 * "TRIAL_NAME": Trial,
 * "ACTIVATION_READY_NAME": Activation Ready,
 * "ACTIVATED_NAME": Activated,
 * "DEACTIVATED_NAME": Deactivated,
 * "RETIRED_NAME": Retired,
 * "PURGED_NAME": Purged,
 * "REPLACED_NAME": Replaced
 * @property float $month_to_date_usage Month to date usage in MB.
 * @property boolean $overage_limit_reached
 * @property string $overage_limit_override Overage Limit Override:
 * "DEFAULT": None
 * "TEMPORARY_OVERRIDE": Current cycle
 * "PERMANENT_OVERRIDE": Ongoing
 * @property integer $date_activated When the terminal is first activated.
 * @property integer $date_added When the terminal is first added to the system.
 * @property integer $date_modified When the terminal is last modified.
 * @property integer $date_shipped When the terminal is shipped.
 * @property float $month_to_date_data_usage Month to date data usage in MB.
 * @property float $month_to_date_sms_usage Month to date SMS usage in message.
 * @property float $month_to_date_voice_usage Month to date voice usage in Secs
 * @property string $secure_sim_id
 * @property string $custom1
 * @property string $custom2
 * @property string $custom3
 * @property integer $rating_term_start_date Term start date and time in UTC if this terminal is on Prepaid plan
 * @property integer $rating_term_end_date Term end date and time in UTC if this terminal is on Prepaid plan
 * @property string $rating_renewal_policy Renewal Policy:
 * "Deactivate": Deactivate,
 * "AutoRenew": Auto Renew,
 * "NamedPlan": Named plan
 * @property string $rating_renewal_rate_plan
 * @property float $rating_total_primary_included_data Amount of data included in MB on the current standard rate plan term, primary zone
 * @property float $rating_primary_data_remaining Amount of data remaining in MB in the current term, primary zone
 * @property float $rating_total_primary_included_sms Amount of SMS included on the current standard rate plan term, primary zone
 * @property float $rating_primary_sms_remaining Amount of SMS remaining in the current term, primary zone
 * @property string $secure_sim_username_copy_rule SecureSIM Username copy rule:
 * "F" :  Unknown
 * "N" :  Not enabled
 * "O" :  Once
 * "A" "  Always
 * "L" :  Locked
 * @property string $secure_sim_password_copy_rule SecureSIM Password copy rule:
 * "F" :  Unknown
 * "N" :  Not enabled
 * "O" :  Once
 * "A" "  Always
 * "L" :  Locked
 * @property integer $account_id Account ID
 * @property string $fixed_ip_address The Fixed IP address of the SIM, in XX.XX.XX.XX format (for example, 12.34.56.78); null if the SIM does not have a Fixed IP address
 * @property integer $ctd_session_count The cycle to date session count for the SIM; null if the session count is not available for the SIM
 * @property string $customer_custom1
 * @property string $customer_custom2
 * @property string $customer_custom3
 * @property string $customer_custom4
 * @property string $customer_custom5
 * @property string $operator_custom1
 * @property string $operator_custom2
 * @property string $operator_custom3
 * @property string $operator_custom4
 * @property string $operator_custom5
 * @property string $imsi
 * @property string $primary_iccid
 * @property string $imei This field may have the device IMEI or IMEISV depending on various factors such as your operator's installation as well as the device itself.
 * @property string $global_sim_type Global SIM Type:
 * "GLOBAL_PRIMARY": Global Primary,
 * "GLOBAL_VIRTUAL": Global Virtual
 * @property string $sim_notes
 * @property integer $version
 * @property string $custom4
 * @property string $custom5
 * @property string $custom6
 * @property string $custom7
 * @property string $custom8
 * @property string $custom9
 * @property string $custom10
 * @property string $msisdn The MSISDN of the SIM.
 *
 * @property Card $card 关联卡
 */
class CardBasic extends ActiveRecord
{
    /**
     * 关联卡
     * @return \yii\db\ActiveQuery
     */
    public function getCard()
    {
        return $this->hasOne(Card::class, ['id' => 'card_id']);
    }

    /**
     * @param \stdClass $data
     */
    public function loadFromRefreshData($data)
    {
        $this->iccid = $data->iccid;
        if (isset($data->terminalId)) {
            $this->terminal_id = $data->terminalId;
        }
        if (isset($data->terminalId)) {
            $this->modem_id = $data->modemId;
        }
        if (isset($data->terminalId)) {
            $this->customer = $data->customer;
        }
        if (isset($data->terminalId)) {
            $this->end_consumer_id = $data->endConsumerId;
        }
        $this->suspended = $data->suspended;
        $this->rate_plan = $data->ratePlan;
        $this->status = $data->status;
        $this->month_to_date_usage = $data->monthToDateUsage;
        $this->overage_limit_reached = $data->overageLimitReached;
        $this->overage_limit_override = $data->overageLimitOverride;
        if (isset($data->dateActivated)) {
            $this->date_activated = strtotime($data->dateActivated);
            $this->card->updateAttributes(['active_time' => strtotime($data->dateActivated)]);
            $this->card->calcTermEnd();
        }
        $this->date_added = strtotime($data->dateAdded);
        $this->date_modified = strtotime($data->dateModified);
        $this->date_shipped = strtotime($data->dateShipped);
        $this->month_to_date_data_usage = $data->monthToDateDataUsage;
        $this->month_to_date_sms_usage = $data->monthToDateSMSUsage;
        $this->month_to_date_voice_usage = $data->monthToDateVoiceUsage;
        $this->secure_sim_id = $data->secureSimId;
        $this->custom1 = $data->custom1;
        $this->custom2 = $data->custom2;
        $this->custom3 = $data->custom3;
        $rating = $data->rating;
        if (isset($rating->termStartDate)) {
            $this->rating_term_start_date = strtotime($rating->termStartDate);
        }
        if (isset($rating->termEndDate)) {
            $this->rating_term_end_date = strtotime($rating->termEndDate);
        }
        if (isset($rating->renewalPolicy)) {
            $this->rating_renewal_policy = $rating->renewalPolicy;
        }
        if (isset($rating->renewalRatePlan)) {
            $this->rating_renewal_rate_plan = $rating->renewalRatePlan;
        }
        if (isset($rating->totalPrimaryIncludedData)) {
            $this->rating_total_primary_included_data = $rating->totalPrimaryIncludedData;
        }
        if (isset($rating->primaryDataRemaining)) {
            $this->rating_primary_data_remaining = $rating->primaryDataRemaining;
        }
        if (isset($rating->totalPrimaryIncludedSMS)) {
            $this->rating_total_primary_included_sms = $rating->totalPrimaryIncludedSMS;
        }
        if (isset($rating->primarySMSRemaining)) {
            $this->rating_primary_sms_remaining = $rating->primarySMSRemaining;
        }
        $this->secure_sim_username_copy_rule = $data->secureSimUsernameCopyRule;
        $this->secure_sim_password_copy_rule = $data->secureSimPasswordCopyRule;
        $this->account_id = $data->accountId;
        $this->fixed_ip_address = $data->fixedIpAddress;
        $this->ctd_session_count = $data->ctdSessionCount;
        $this->customer_custom1 = $data->customerCustom1;
        $this->customer_custom2 = $data->customerCustom2;
        $this->customer_custom3 = $data->customerCustom3;
        $this->customer_custom4 = $data->customerCustom4;
        $this->customer_custom5 = $data->customerCustom5;
        $this->operator_custom1 = $data->operatorCustom1;
        $this->operator_custom2 = $data->operatorCustom2;
        $this->operator_custom3 = $data->operatorCustom3;
        $this->operator_custom4 = $data->operatorCustom4;
        $this->operator_custom5 = $data->operatorCustom5;
        $this->imsi = $data->imsi;
        if (isset($data->primaryICCID)) {
            $this->primary_iccid = $data->primaryICCID;
        }
        if (isset($data->imei)) {
            $this->imei = $data->imei;
        }
        if (isset($data->globalSimType)) {
            $this->global_sim_type = $data->globalSimType;
        }
        if (isset($data->simNotes)) {
            $this->sim_notes = $data->simNotes;
        }
        $this->version = $data->version;
        $this->custom4 = $data->custom4;
        $this->custom5 = $data->custom5;
        $this->custom6 = $data->custom6;
        $this->custom7 = $data->custom7;
        $this->custom8 = $data->custom8;
        $this->custom9 = $data->custom9;
        $this->custom10 = $data->custom10;
        if (isset($data->msisdn)) {
            $this->msisdn = $data->msisdn;
            $this->card->updateAttributes(['msisdn' => $this->msisdn]);
        }
        if (isset($data->imsi)) {
            $this->imsi = $data->imsi;
            $this->card->updateAttributes(['imsi' => $this->imsi]);
        }

        $statusMap = [
            'TEST_READY_NAME' => Card::STATUS_TEST_READY,
            'INVENTORY_NAME' => Card::STATUS_INVENTORY,
            'TRIAL_NAME' => Card::STATUS_INVENTORY,
            'ACTIVATION_READY_NAME' => Card::STATUS_ACTIVATION_READY,
            'ACTIVATED_NAME' => Card::STATUS_ACTIVATED,
            'DEACTIVATED_NAME' => Card::STATUS_DEACTIVATED,
            'RETIRED_NAME' => Card::STATUS_RETIRED,
            'PURGED_NAME' => Card::STATUS_RETIRED,
            'REPLACED_NAME' => Card::STATUS_RETIRED,
        ];
        if (isset($statusMap[$this->status])) {
            $this->card->updateAttributes(['status' => $statusMap[$this->status]]);
        }
        $this->save();

        $data = $this->month_to_date_data_usage * 1024 * 1024;
        $sms = $this->month_to_date_sms_usage;
        $voice = $this->month_to_date_voice_usage / 60;
        $this->card->updateAttributes(['data' => $data, 'sms' => $sms, 'voice' => $voice]);
    }
}
